// pages/baocanList/baocanList.js
const urlAdd = require('../../utils/config').urlAdd;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mealOrderList: [], //报餐列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // that.getMyMealList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.getMyMealList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //我的报餐列表
  getMyMealList: function () {
    wx.showLoading({
      title: '加载中...',
    })
    let that = this;
    wx.request({
      url: urlAdd + '/f/wechat/meal/findMealOrderByWeixinOpenId',
      data: {
        weixinOpenId: wx.getStorageSync('openId'), //openId
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading();
        if (res.data.code == 200) {
          that.setData({
            mealOrderList: res.data.data.list
          })
        } else {
          wx.showToast({
            title: res.data.message,
            image: '/images/erron.png',
            duration: 2000
          });
        }

      },
      fail: function () {
        wx.hideLoading();
        wx.showToast({
          title: '系统错误',
          image: '/images/erron.png',
          duration: 2000
        });
      }
    })
  },

  //取消
  cancelMealOrder: function (e) {
    var that = this;
    console.log(e)
    var orderId = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '确认取消？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: urlAdd + '/f/wechat/meal/cancelMealOrder',
            data: {
              weixinOpenId: wx.getStorageSync('openId'), //openId
              orderId: orderId
            },
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              console.log(res)
              if (res.data.code == 200) {

                wx.showToast({
                  title: '取消成功',
                  duration: 2000,
                  success: function () {
                    //重新加载
                    that.setData({
                      mealOrderList: []
                    });
                    that.getMyMealList();
                  }
                });

              } else {
                wx.showToast({
                  title: res.data.message,
                  image: '/images/erron.png',
                  duration: 2000
                });
              }

            },
            fail: function () {
              wx.hideLoading();
              wx.showToast({
                title: '系统错误',
                image: '/images/erron.png',
                duration: 2000
              });
            }
          })
        } else if (res.cancel) {

        }
      }
    })
  },
})